﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Specialized;
using WebNext.UI.Models.Comments;

namespace WebNext.UI.Utils.ModelBinders
{
	public class CommentModelBinder : IModelBinder
	{
		public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
		{
			if (bindingContext.Model != null)
			{
				throw new NotImplementedException("Cannot update instances");
			}
			return this.BindCommentModel(controllerContext.HttpContext.Request.Form);
		}

		private CommentViewModel BindCommentModel(NameValueCollection form)
		{
			int id = Convert.ToInt32(form["id"]);
			int entryId = Convert.ToInt32(form["entryId"]);
			string author = form["author"];
			string content = form["content"];
			CommentViewModel commentViewModel = new CommentViewModel(id, entryId, author, content);
			commentViewModel.CaptchaKey = form["captchaKey"];
			commentViewModel.CaptchaValue = form["captchaValue"];
			return commentViewModel;
		}
	}
}